﻿using AlumniProject.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AlumniProject
{
    public partial class DangKy1 : System.Web.UI.Page
    {
        private DangKyCSV csv;
        private DangKyGiangVien giangvien;
        TaiKhoanAccess _taikhoan = new TaiKhoanAccess();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Page_Init(sender,e);
                pnlGV.Visible = false;
            }
        }
        public void Page_Init(object o, EventArgs e)
        {
            if (!IsPostBack)
            { String controlname = GetPostBackControlId(this.Page); }
        }

        public string GetPostBackControlId(Page page)
        {
            if (!page.IsPostBack)
                return string.Empty;

            Control control = null;
            // first we will check the "__EVENTTARGET" because if post back made by the controls
            // which used "_doPostBack" function also available in Request.Form collection.
            string controlName = page.Request.Params["__EVENTTARGET"];
            if (!String.IsNullOrEmpty(controlName))
            {
                control = page.FindControl(controlName);
            }
            else
            {
                // if __EVENTTARGET is null, the control is a button type and we need to
                // iterate over the form collection to find it

                // ReSharper disable TooWideLocalVariableScope
                string controlId;
                Control foundControl;
                // ReSharper restore TooWideLocalVariableScope

                foreach (string ctl in page.Request.Form)
                {
                    // handle ImageButton they having an additional "quasi-property" 
                    // in their Id which identifies mouse x and y coordinates
                    if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
                    {
                        controlId = ctl.Substring(0, ctl.Length - 2);
                        foundControl = page.FindControl(controlId);
                    }
                    else
                    {
                        foundControl = page.FindControl(ctl);
                    }

                    //lvdinh
                    if (ctl.Contains("btnDangKyGV")) //tim thay nut dang ki giang vien
                    {
                        PlaceHolder o2 = (PlaceHolder)Session["PlaceHolder"];
                        if (o2 != null)
                        {
                            if (giangvien != null)
                            {

                            }
                            DangKyGiangVien dkgv = (DangKyGiangVien)Session["DangKyGiangVien"];
                            string str = dkgv.GetText();
                            if (dkgv != null)
                            {
                                dkgv.dkGiangVien();
                            }
                        }

                    }
                    else if (ctl.Contains("btnDangKyCSV")) //tim thay nut dang ki csv
                    {

                    }
                    //het lvdinh

                    if (!(foundControl is Button || foundControl is ImageButton)) continue;

                    control = foundControl;
                    break;
                }
            }

            return control == null ? String.Empty : control.ID;
        }

        private Control con;
        protected void btnDangKy_Click(object sender, EventArgs e)
        {
            var username = txtTenDangNhap.Text;
            var matkhau = txtMatKhau.Text;
            var email = txtEmail.Text;
            var tk = new TaiKhoan { TenDangNhap = username, MatKhau = matkhau, Email = email };
            Session["taikhoandk"] = tk;
            switch (rblDangKy.SelectedValue)
            {
                case "csv":
                    pnlCSV.Visible = true;
                    pnlGV.Visible = false;

                    var tinhthanhsv = _taikhoan.TinhThanhFull();
                    ddrTinh.DataSource = tinhthanhsv;
                    ddrTinh.DataValueField = "MaTinh";
                    ddrTinh.DataTextField = "TenTinh";
                    ddrTinh.DataBind();

                    ddrLinhVuc.DataSource = _taikhoan.LinhVucCTFull();
                    ddrLinhVuc.DataValueField = "MaLV_CT";
                    ddrLinhVuc.DataTextField = "TenLV_CT";
                    ddrLinhVuc.DataBind();

                    ddrNoiCongTac.DataSource = tinhthanhsv;
                    ddrNoiCongTac.DataValueField = "MaTinh";
                    ddrNoiCongTac.DataTextField = "TenTinh";
                    ddrNoiCongTac.DataBind();


                    break;
                case "gv":
                    pnlGV.Visible = true;
                    pnlCSV.Visible = false;

                    var tinhthanh = _taikhoan.HocViFull();
                    ddrHocVi.DataSource = tinhthanh;
                    ddrHocVi.DataValueField = "MaHocVi";
                    ddrHocVi.DataTextField = "TenHocVi";
                    ddrHocVi.DataBind();

                    Session["PlaceHolder"] = UserReg;
                    break;
            }

            rq1.Visible = rq2.Visible = rq3.Visible = rq4.Visible = rq5.Visible = false;
            btnDangKy.Enabled = rblDangKy.Enabled = false;
            txtTenDangNhap.Enabled = txtMatKhau.Enabled = txtConfirmPassword.Enabled = txtEmail.Enabled = false;

            //DangKyGiangVien dkgv = UserReg.FindControl()
        }


        private PlaceHolder getPlaceHolder()
        {
            return UserReg;
        }

        protected void btnDangKyGV_Click(object sender, EventArgs e)
        {
            dkGiangVien();
        }
        public void dkGiangVien()
        {
            TaiKhoan tk = (TaiKhoan)Session["taikhoandk"];
            GiangVien gv = new GiangVien();

            gv.Ten = txtTenGV.Text;
            gv.HocVi = ddrHocVi.SelectedValue;
            gv.SDT = txtSoDTGV.Text;
            gv.Ma = txtMaGV.Text;
            if (rbtGioiTinhGV.SelectedIndex == 0)
                gv.GioiTinh = true;
            else
            {
                gv.GioiTinh = false;
            }
            DateTime d;
            DateTime.TryParse(txtNgaySinhGV.Text, out d);
            gv.NgaySinh = d;

            bool res = _taikhoan.DangKyTaiKhoan(tk, "gv", gv, null);

            Response.Write(res
                ? "<script>alert('Đăng ký thành công, chờ kiểm duyệt!'); "
                    + "window.navigate='DangNhap.aspx';</script>"
                : "<script>alert('Đăng ký thất bại!');</script>");
        }

        protected void btnDangKyCSV_Click(object sender, EventArgs e)
        {
            TaiKhoan tk = (TaiKhoan)Session["taikhoandk"];
            Csv csv = new Csv();

            csv.Ten = txtTenCSV.Text;
            if (rbtGioiTinh.SelectedIndex == 0)
                csv.GioiTinh = true;
            else
            {
                csv.GioiTinh = false;
            }
            csv.Cmnd = txtCMNDCSV.Text;

            DateTime d;
            DateTime.TryParse(txtNgaySinh.Text, out d);
            csv.NgaySinh = d;
            csv.SoNha = txtSoNha.Text;
            csv.MaTinh = ddrTinh.SelectedValue;
            csv.Sdt = txtSoDT.Text;
            csv.MaLv = ddrLinhVuc.SelectedValue;
            csv.NoiCongTac = ddrNoiCongTac.SelectedValue;


            bool res = _taikhoan.DangKyTaiKhoan(tk, "csv", null, csv);
            Response.Write(res
                ? "<script>alert('Đăng ký thành công, chờ kiểm duyệt!');</script>"
                : "<script>alert('Đăng ký thất bại!');</script>");
        }
    }
}